package com.zhenxiang.juc.lock.productComsumer;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author : James
 * @Create : 2021-12-21-11:47
 * @Description : Condition 实现精准的通知 唤醒线程
 *
 *   线程A 执行完调用线程B ，线程B执行完调用线程C，线程C执行完调用线程A
 */
public class ProductConsumerLock3 {

    public static void main(String[] args) {
       Data3  data3 = new Data3();


        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data3.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data3.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();


        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data3.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();


    }
}

// 线程资源类
class  Data3 {

    // 1 =A 、2=B、 3=C
    private int num = 1;
    Lock lock = new ReentrantLock();
    // 监视器
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();


    public void printA() throws InterruptedException {
        lock.lock();
        try {
            while (num != 1) {
                // 等待
                condition1.await();
            }
            num =2;
            System.out.println(Thread.currentThread().getName()  +"=====>"+ "AAAAAAAAAAA" );
            // 唤醒等待的B 线程
            condition2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    public void printB() throws InterruptedException {
        lock.lock();
        try {
            while (num !=2) {
                condition2.await();
            }
            num=3;
            System.out.println(Thread.currentThread().getName() +"=====>"+ "BBBBBBB" );
            // 唤醒等待的C 线程
            condition3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printC() throws InterruptedException {
        lock.lock();
        try {
            while (num !=3) {
                condition3.await();
            }
            num=1;
            System.out.println(Thread.currentThread().getName()  +"=====>"+ "CCCCCCCCC" );
            // 唤醒等待的A线程
            condition1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}



